package stdio.costa.moca.rest;

import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import weibo4j.Oauth;
import weibo4j.http.AccessToken;
import weibo4j.model.WeiboException;

@Path("/authorization")
public class AuthorizationResource {

	Oauth oauth = new Oauth();

	@Path("/getRequestTokenUrl")
	@GET
	@Produces({ "application/json" })
	public String getRequestTokenUrl() throws Exception {
		String url = oauth.authorize("code", "123");
		return url;
	}

	@Path("/getAccessToken")
	@POST
	@Produces({ "application/json" })
	@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
	public String getAccessToken(@FormParam("code") String code) throws WeiboException {
		System.out.println(code);

		AccessToken accessToken = oauth.getAccessTokenByCode(code);

		System.out.println("Access token: " + accessToken.getAccessToken());

		String access_token = accessToken.getAccessToken();

		return access_token;
	}
}
